//数据检测者，用来拦截数据变化
;
(function(win) {
    function Observer(data) {
        this._data = data;
        this.walk();
    }

    Observer.prototype = {
        walk: function() {
            Object.keys(this._data).forEach(function(key) {
                this.defineRactive(this._data, key, this._data[key]);
            }.bind(this));
        },
        defineRactive: function(data, key, val) {
            var dispatcher = new Dispatcher();
            var childObj = observe(val);
            var me = this;
            Object.defineProperty(data, key, {
                configurable: true,
                enumerable: true,
                get: function() {
                    //添加订阅者
                    if (Dispatcher.target) {
                        dispatcher.depend();
                    }
                    return val;
                },
                set: function(newVal) {
                    if (newVal === val) {
                        return;
                    }
                    val = newVal;
                    me.walk(childObj);
                    //通知订阅者
                    dispatcher.notify();
                }
            });
        }
    };

    function observe(data) {
        if (data && typeof data !== "object") {
            return;
        }
        return new Observer(data);
    }

    win.Observer = Observer;
    win.observe = observe;
})(window);